﻿@inject NavigationManager NavigationManager
@inject IHttpService _httpService

@inject IDataCacheService _dataCatcheService
@inject IUserService _userService


@if (isReady)
{

    @if (Model.Peripheries.Count != 0)
    {
        <CnGalWebSite.Components.Containers.MasaTitleContainer Title="周边" Icon="@IconType.Periphery.ToIconString()" Outline OnlyShowContext="OnlyShowContext">
             <MDCard Outline="Outline" Class="@($"periphery-overview-card {Class}")">
                 <MDCard Outline Href="@GetUrl()" OnClick="OnClick" Style="flex-direction: row;">
                         @if (Model.IsThumbnail)
                        {
                        <div class="ms-3 mt-3 pe-0 image-card" style=" display: flex; align-items: center;">
                                <img src="@Model.Image" alt="@Model.Name" style="aspect-ratio: 1/1; height: 75px; border-radius: 50%;" />
                            </div>
                        }
                        else
                        {
                            <div class="ps-3 pt-3 pe-0 image-card">
                                <img src="@Model.Image" alt="@Model.Name" style="aspect-ratio: 460/215; height: 75px;" />
                            </div>
                        }
                        <div class="ps-3 pt-3 pe-4 proc-card">
                            <div style=" display: flex; flex-wrap: wrap; flex-direction: column; justify-content: center;" class="w-100 h-100">
                                <div style="display: flex; align-items: flex-end; justify-content: flex-start; flex-wrap: wrap;" class="mb-2">
                                    <div class="title-large me-2">@Model.Name</div>

                                    <div>@("收集进度 " + Model.Peripheries.Count(s => s.IsCollected == true) + "/" + Model.Peripheries.Count)</div>
                                </div>
                                <div class="label-large text-truncate-2">@Model.BriefIntroduction</div>

                            </div>
                        </div>
                 </MDCard>
                <div class="ms-3 me-3 mb-1 mt-1 pt-2">
                    <MProgressLinear Value="@((int)((double)Model.Peripheries.Count(s=>s.IsCollected==true)*100/Model.Peripheries.Count))" BufferValue="0" Stream />
                </div>
                 <MSlideGroup ShowArrows="true" Class="ms-3 me-2 pb-3 slide-group-garp">
                    <ChildContent>
                        @foreach (var item in Model.Peripheries)
                        {
                            <MSlideItem>
                                <div style="width: 150px;padding-top: .5rem;padding-bottom: .5rem;">
                                    <PeripheryOverviewAloneTip Model="item" />
                                </div>
                            </MSlideItem>
                        }
                    </ChildContent>

                </MSlideGroup>
            </MDCard>
        </CnGalWebSite.Components.Containers.MasaTitleContainer>
    }
    else
    {
        if (IsGameView)
        {
            <CnGalWebSite.Components.Containers.MasaTitleContainer Title="周边" Icon="@IconType.Periphery.ToIconString()" Outline>

                <AuthorizeView>
                    <Authorized>
                     <MDCard Class="pa-3 mb-4 w-100" Style="align-items: center;">
                         <CnGalWebSite.Components.Buttons.MasaButton Rounded Text="创建周边" OnClick="@(()=>NavigationManager.NavigateTo("/peripheries/createperiphery"))" Icon="@IconType.Create.ToIconString()" />
                        </MDCard>
                    </Authorized>
                    <NotAuthorized>
                     <MDCard Class="pa-3 mb-4 w-100" Style="align-items: center;">
                             <CnGalWebSite.Components.Buttons.MasaButton Rounded Text="登入后创建周边" OnClick="_userService.Login" Icon="@IconType.Login.ToIconString()" />
                     </MDCard>
                 </NotAuthorized>
                </AuthorizeView>
            </CnGalWebSite.Components.Containers.MasaTitleContainer>
        }
    }

}

@code {
    [Parameter]
    public int EntryId { get; set; }
    [Parameter]
    public bool IsEdit { get; set; }
    [Parameter]
    public bool Outline { get; set; }
    [Parameter]
    public bool IsGameView { get; set; }
    [Parameter]
    public string Class { get; set; }
    [Parameter]
    public bool OnlyShowContext { get; set; }

    bool isReady = false;
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    [Parameter]
    public GameOverviewPeripheryListModel Model { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        if (EntryId > 0)
        {
            try
            {
                Model = await _httpService.GetAsync<GameOverviewPeripheryListModel>("api/peripheries/GetEntryOverviewPeripheries/" + EntryId);
                isReady = true;
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取词条相关周边集合概览失败");
            }
        }
        else
        {
            isReady = true;
        }


    }

    public string GetUrl()
    {
        string url = null;
        if (Model.Type == PeripheryOverviewType.Entry)
        {
            url = "/entries/index/" + Model.ObjectId;
        }
        else if (Model.Type == PeripheryOverviewType.Periphery)
        {
            url = "/peripheries/index/" + Model.ObjectId;
        }
        else if (Model.Type == PeripheryOverviewType.User)
        {
            url = string.IsNullOrWhiteSpace(Model.ObjectId) ? null : ("/space/index/" + Model.ObjectId + "?TabIndex=11");
        }

        return url;
    }

    public void OnClick()
    {
        if(GetUrl()==null)
        {
            _userService.Login();
        }
    }
}
